package com.jxb.jingchen.print;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.jxb.dialog.BaseDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jinxb
 * date     : 2021/8/19
 * desc     :
 */
public class PrinterDialog extends BaseDialog  {
    private final PrinterAdapter.OnItemClickListener onItemClickListener;
    private RecyclerView mRecycler;
    private AppCompatTextView mCancelTextView;
    private PrinterAdapter adapter;
    private List<BluetoothDevice> deviceList;

    public PrinterDialog(@NonNull Context context, PrinterAdapter.OnItemClickListener onItemClickListener) {
        super(context);
        deviceList = new ArrayList<>();
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    protected int getContentView() {
        return R.layout.dialog_printer;
    }

    @Override
    protected void initViews() {
        mRecycler = findViewById(R.id.recycler);
        mCancelTextView = findViewById(R.id.cancelTextView);
    }

    @Override
    protected void initListeners() {
        mCancelTextView.setOnClickListener(view -> dismiss());
    }

    private PrinterAdapter getAdapter() {
        if (adapter == null) {
            adapter = new PrinterAdapter(getContext(), deviceList);
            mRecycler.setAdapter(adapter);
            adapter.setOnItemClickListener(onItemClickListener);
        }
        return adapter;
    }

    public void addDevice(BluetoothDevice device) {
        deviceList.add(device);
        getAdapter().notifyDataSetChanged();
    }
}
